<%@ Page Title="" Debug="true" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master"
    Inherits="System.Web.Mvc.ViewPage" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="CDIO1.Models" %>

<script runat="server">

    protected void lbInsert_Click(object sender, EventArgs e)
    {
        var db = new MethodDataContext();
        TeachingMethodLanguage record = new TeachingMethodLanguage();
        record.LanguageID = int.Parse((GridView1.FooterRow.FindControl("ddlLanguage") as DropDownList).SelectedValue);
        record.TeachingMethodID = int.Parse((GridView1.FooterRow.FindControl("ddlMethod") as DropDownList).SelectedValue);
        record.Method = ((TextBox)GridView1.FooterRow.FindControl("txtMethod")).Text;
        record.Status = (int)Status.AVAILABLE;
        db.TeachingMethodLanguages.InsertOnSubmit(record);
        db.SubmitChanges();
        Response.Redirect("~/Admin/ManageTeachMethodLanguage");
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            Label t = GridView1.Rows[i].FindControl("Status") as Label;
            Label t1 = GridView1.Rows[i].FindControl("lbStatus") as Label;
            if (t.Text == "1")
            {
                t1.Text = "Available";
            }
            if (t.Text == "0")
            {
                t1.Text = "Unavailable";
            }
        }
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    ManageTeachMethodLanguage
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h1>
        ManageTeachMethodLanguage</h1>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="#333333"
                GridLines="None" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <Columns>
                    <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID" Visible="False">
                        <EditItemTemplate>
                            <asp:Label ID="lbID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="TeachingMethodID" SortExpression="TeachingMethodID"
                        Visible="False">
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("TeachingMethodID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MethodID" SortExpression="MethodID">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlMethod" runat="server" DataSourceID="SqlDataSourceMethod"
                                DataTextField="MethodID" DataValueField="ID" SelectedValue='<%# Bind("TeachingMethodID") %>'>
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="SqlDataSourceMethod" runat="server" ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>"
                                SelectCommand="SELECT ID, MethodID, Status FROM TeachingMethod WHERE (Status = 1)">
                            </asp:SqlDataSource>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="ddlMethod" runat="server" DataSourceID="SqlDataSourceMethod"
                                DataTextField="MethodID" DataValueField="ID" SelectedValue='<%# Bind("TeachingMethodID") %>'>
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="SqlDataSourceMethod" runat="server" ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>"
                                SelectCommand="SELECT ID, MethodID, Status FROM TeachingMethod WHERE (Status = 1)">
                            </asp:SqlDataSource>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# Bind("MethodID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description" SortExpression="Method">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtMethod" runat="server" Text='<%# Bind("Method") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtMethod" runat="server" Text='<%# Bind("Method") %>'></asp:TextBox>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("Method") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="LanguageID" SortExpression="LanguageID" Visible="False">
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# Bind("LanguageID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Language" SortExpression="Name">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlLanguage" runat="server" DataSourceID="SqlDataSourceLanguage"
                                DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("LanguageID") %>'>
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="SqlDataSourceLanguage" runat="server" ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>"
                                SelectCommand="SELECT ID, Name, Status FROM Language WHERE (Status = 1)"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="ddlLanguage" runat="server" DataSourceID="SqlDataSourceLanguage"
                                DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("LanguageID") %>'>
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="SqlDataSourceLanguage" runat="server" ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>"
                                SelectCommand="SELECT ID, Name, Status FROM Language WHERE (Status = 1)"></asp:SqlDataSource>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label5" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Status" SortExpression="Status">
                        <EditItemTemplate>
                            <asp:Label ID="Status" runat="server" Text='<%# Bind("Status") %>' Visible="False"></asp:Label>
                            <asp:Label ID="lbStatus" runat="server" Text="Label"></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Status" runat="server" Text='<%# Bind("Status") %>' Visible="False"></asp:Label>
                            <asp:Label ID="lbStatus" runat="server" Text="Label"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="False">
                        <EditItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                Text="Update"></asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="Cancel"></asp:LinkButton>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:LinkButton ID="lbInsert" runat="server" OnClick="lbInsert_Click" ForeColor="White">Insert</asp:LinkButton>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="Edit"></asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                                Text="Delete"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>"
                DeleteCommand="UPDATE [TeachingMethodLanguage] SET [Status] = 0 WHERE [ID] = @ID"
                InsertCommand="INSERT INTO [TeachingMethodLanguage] ([TeachingMethodID], [Method], [LanguageID], [Status]) VALUES (@TeachingMethodID, @Method, @LanguageID, @Status)"
                SelectCommand="SELECT TeachingMethodLanguage.ID, TeachingMethodLanguage.TeachingMethodID, TeachingMethodLanguage.Method, TeachingMethodLanguage.LanguageID, TeachingMethodLanguage.Status, TeachingMethod.MethodID, Language.Name FROM TeachingMethodLanguage INNER JOIN TeachingMethod ON TeachingMethodLanguage.TeachingMethodID = TeachingMethod.ID INNER JOIN Language ON TeachingMethodLanguage.LanguageID = Language.ID"
                UpdateCommand="UPDATE [TeachingMethodLanguage] SET [TeachingMethodID] = @TeachingMethodID, [Method] = @Method, [LanguageID] = @LanguageID, [Status] = @Status WHERE [ID] = @ID">
                <DeleteParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="TeachingMethodID" Type="Int32" />
                    <asp:Parameter Name="Method" Type="String" />
                    <asp:Parameter Name="LanguageID" Type="Int32" />
                    <asp:Parameter Name="Status" Type="Int32" />
                    <asp:Parameter Name="ID" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="TeachingMethodID" Type="Int32" />
                    <asp:Parameter Name="Method" Type="String" />
                    <asp:Parameter Name="LanguageID" Type="Int32" />
                    <asp:Parameter Name="Status" Type="Int32" />
                </InsertParameters>
            </asp:SqlDataSource>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdatePanelAnimationExtender ID="UpdatePanelAnimationExtender1" runat="server"
        TargetControlID="UpdatePanel1">
        <Animations>
            <OnUpdating>                
                <FadeOut minimumOpacity=".5" />
            </OnUpdating>
            <OnUpdated>
                <FadeIn minimumOpacity=".5" />
            </OnUpdated>
        </Animations>
    </asp:UpdatePanelAnimationExtender>
    <asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="0" AssociatedUpdatePanelID="UpdatePanel1">
        <ProgressTemplate>
            <img style="margin-left: 50px" src="../../App_Themes/AdminTheme/images/ajax-loader.gif" />
        </ProgressTemplate>
    </asp:UpdateProgress>
</asp:Content>
